#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<stdio.h>

const int PORT = 9506;
const char server_ip[15] = {"127.0.0.1"};

int main()
{
    //创建socket对象
    int sockfd=socket(AF_INET,SOCK_DGRAM,0);

    //创建网络通信对象
    struct sockaddr_in addr;
    addr.sin_family =AF_INET;
    addr.sin_port =htons(PORT);
    addr.sin_addr.s_addr = inet_addr(server_ip);

    while(1)
    {
        printf("请输入您要发送的内容: \n");
        char buf[64]= {0};
        scanf("%s",buf);
        sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&addr,sizeof(addr));

        socklen_t len=sizeof(addr);
        recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&addr,&len);
        printf("接收到的数据:%s\n",buf);
    }
    close(sockfd);

}
